﻿using Gx.Model.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gx.Dal.Extensions;
using Dapper.Contrib.Extensions;
using Dapper;
using Gx.Common.Core;

namespace Gx.Dal.Manage
{
    public class LKZeroWarehouseCostDetailDal : BaseDal<LKZeroWarehouseCostDetail>
    {
        public void AddZeroCostDetail(string contractNo, string billNo, DateTime beginTime, DateTime endTime, int days, decimal unitPrice)
        {
            UseTransaction((conn, trans) =>
            {
                //var endtimestr = conn.QueryFirstOrDefault<string>("SELECT TOP 1 EndTime  FROM dbo.LK_WarehouseEntry WITH(NOLOCK) WHERE RentalWarehouseType=1 AND IsDeleted=0 AND BillNo=@billNo ORDER BY EndTime DESC", new { billNo }, trans);

                if (endTime.Year!=1911)
                {
                    var lastBeginTime = endTime.AddDays(1);

                    if (lastBeginTime.ToString("yyyy-MM-dd") == beginTime.ToString("yyyy-MM-dd"))
                    {
                        var contractAny = conn.GetListByWhere<LKZeroWarehouseCostDetail>("BillNo=@billNo AND CONVERT(VARCHAR,BeginTime, 23) =@beginTime", new { billNo, beginTime = lastBeginTime.ToString("yyyy-MM-dd") }, trans).Any();
                        if (!contractAny)
                        {
                            conn.Insert(new LKZeroWarehouseCostDetail
                            {
                                ContractNo = contractNo,
                                BillNo = billNo,
                                BeginTime = beginTime,
                                EndTime = lastBeginTime.AddDays(days),
                                UnitPrice = unitPrice,
                                TotalPrice = unitPrice,
                                CreateTime = DateTime.Now,
                                IsDeleted = false
                            }, trans);

                            var nextendTime = beginTime.AddDays(days);
                            conn.Execute("UPDATE LK_WarehouseEntry SET EndTime=@endTime WHERE ContractNo=@contractNo AND IsDeleted=0", new { endTime= nextendTime, contractNo }, trans);
                        }
                    }

                }


                return true;
            });
            //var contractAny = GetListByWhere("ContractNo=@contractNo AND CONVERT(VARCHAR,BeginTime, 23) =@beginTime", new { contractNo, beginTime = beginTime.ToString("yyyy-MM-dd") }).Any();
            //if (!contractAny)
            //{
            //    Insert(new LKZeroWarehouseContractDetail
            //    {
            //        ContractNo = contractNo,
            //        BeginTime = beginTime,
            //        EndTime = beginTime.AddDays(days),
            //        UnitPrice = unitPrice,
            //        TotalPrice = unitPrice,
            //        CreateTime = DateTime.Now,
            //        IsDeleted = false
            //    });
            //}
        }

        public List<LKZeroWarehouseCostDetail> GetZeroEntryByBalance()
        {
            using (var conn = new DbContext())
            {
                return conn.Connection.Query<LKZeroWarehouseCostDetail>(@"SELECT DISTINCT e.BillNo,e.ContractNo,e.ColdUnitPrice as UnitPrice,e.BeginTime,e.EndTime,c.Days
FROM dbo.LK_WarehouseEntryDetail d WITH(NOLOCK)
JOIN dbo.LK_WarehouseEntry e WITH(NOLOCK) ON d.EntryId=e.Id
LEFT JOIN dbo.LK_ZeroWarehouseContract c WITH(NOLOCK) ON e.ContractNo=c.ContractNo
LEFT JOIN (SELECT GoodsNo,SUM(OutNumber) AS OutNumber,SUM(TonWeight) AS TonWeight FROM dbo.LK_WarehouseOutDetail WITH(NOLOCK)
WHERE IsDeleted=0
GROUP BY GoodsNo) AS  od  ON d.GoodsNo=od.GoodsNo
WHERE d.IsDeleted=0 AND e.IsDeleted=0 AND e.RentalWarehouseType=1 AND (od.GoodsNo IS NULL OR d.Number>od.OutNumber)
").ToList();
            }
        }
    }
}
